import com.sun.net.httpserver.HttpExchange;

import java.util.Map;

/**
 * 基础控制器 - 包含路由器和路径参数
 */
public abstract class BaseController{

    // 路径参数
    protected Map<String, String> pathParams;

    private HttpExchange currentExchange;


    public HttpExchange getCurrentExchange() {
        return currentExchange;
    }

    public void setCurrentExchange(HttpExchange currentExchange) {
        this.currentExchange = currentExchange;
    }

    /**
     * 设置路径参数
     */
    public void setPathParams(Map<String, String> pathParams) {
        this.pathParams = pathParams;
    }

    /**
     * 抽象方法：处理请求
     */
    public abstract String handleRequest();

    // 工具方法
    protected String getPathParam(String name) {
        return pathParams != null ? pathParams.get(name) : null;
    }

    protected Integer getIntPathParam(String name) {
        String value = getPathParam(name);
        if (value == null) return null;
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return null;
        }
    }
}